package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IMenu;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.Menu;

public class MenuAction extends MyBaseAction<Menu> implements ModelDriven<Menu> {
	private IMenu menuImpl;

	public IMenu getMenuImpl() {
		return menuImpl;
	}

	public void setMenuImpl(IMenu menuImpl) {
		this.menuImpl = menuImpl;
	}

	public MenuAction() {
		super(Menu.class);
		validRules.put("title", "empty");
		validRules.put("menuId", "empty");
		validRules.put("ids", "empty");
	}

	public Menu getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	// 查找所有的菜单
	public void findAllMenu() {
		String r = "";
		if (model.getTitle() == null) {
			try {
				r = menuImpl.findByProperty("Menu",
						"*,menu.title(ParentTitle)", "json", true, null, null,
						null, null, model.getPage(), model.getRows());
			} catch (Exception e) {
				r = errorHandle.handleErr(e);
			}
		} else {
			try {
				r = menuImpl.findByProperty("Menu",
						"*,menu.title(ParentTitle)", "json", true,
						new String[] { "title" }, new Object[] { model
								.getTitle() }, null, new String[] { "like" },
						model.getPage(), model.getRows());
			} catch (Exception e) {
				r = errorHandle.handleErr(e);
			}
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

	// 添加菜单
	public void addMenu() {
		if (!validMe("title")) {
			return;
		}
		String r = "";
		r = menuImpl.addMenu(model);
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	// 修改菜单
	public void editMenu() {
		if (!validMe("menuId")) {
			return;
		}
		String r = "";
		try {
			r = menuImpl.editMenu(model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	// 删除菜单
	public void delMenuByIds() {
		if (!validMe("ids")) {
			return;
		}
		String r = "";
		try {
			r = menuImpl.delMenuByIds(model.getIds());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	// 根据id获取某个menu的信息
	public void getMenuById() {
		if (!validMe("menuId")) {
			return;
		}
		String r = "";
		try {
			r = menuImpl.findByProperty("Menu", "*,menu.menuId(parentId)",
					"json", false, new String[] { "menuId" },
					new Object[] { model.getMenuId() }, null, null, 0, 0);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

	// 获取全部的父menu
	public void getParentMenu() {
		String r = "";
		try {
			r = menuImpl.findByProperty("Menu", "*", "json", true,
					new String[] { "menu" }, new Object[] { null }, null,
					new String[] { "is null" }, 0, 0);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

}
